<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of pagination
 *
 * @author IT
 */
class pagination extends connection {

    function phan_trang() { 
        $sobai =  mysql_num_rows($this->ketnoi($query = "SELECT * FROM articles"));;
        $per_page = 2;
        if(!isset($_GET['page'])){
            $pageindex = 1;
        }  else if ($_GET['page']>  ceil($sobai/$per_page)){
            $_GET['page'] = 1;
            $pageindex = $_GET['page'];
        }  else {
            $pageindex = $_GET['page'];
        }
        if ($sobai == 0) {
            echo "chưa có bài viết";
        } else {

            //first
            $firt = 1;
            echo $trang2 = '<li><a class="' . 'first' . '" href="index.php?page=' . $firt . '">' . 'First' . '</a></li>';
            //prev
            $page2;
            $prev;
            if (!isset($_GET['page'])) {
                $page2 = 1;
            } else {
                $page2 = $_GET['page'];
            }
            $prev = $page2 - 1;
            if ($prev < 1) {
                $prev = 1;
            }
            echo $trang4 = '<li><a class="' . 'previous' . '" href="index.php?page=' . $prev . '">' . 'Previous' . '</a></li>';
            //page
            for ($i = 1; $i <= ceil
                            ($sobai / $per_page); $i++) {
                if ($pageindex == $i) {
                    $trang = '<li>' . '<a href="index.php?page=' . $i . '">' . '<b>' . $i . '</b></a></li>';
                    echo $trang;
                } else {
                    $trang = '<li>' . '<a href="index.php?page=' . $i . '">' . $i . '</a></li>';
                    echo $trang;
                }
            }
            //next
            $page1;
            $next;
            if (!isset($_GET['page'])) {
                $page1 = 1;
            } else {
                $page1 = $_GET['page'];
            }
            $next = $page1 + 1;
            if ($next > ceil($sobai / $per_page)) {
                $next = ceil($sobai / $per_page);
            }
            echo $trang3 = '<li><a class="' . 'next' . '" href="index.php?page=' . $next . '">' . 'Next' . '</a></li>';
            //last

            $last = ceil($sobai / $per_page);
            $trang1 = '<li><a class="' . 'last' . '" href="index.php?page=' . $last . '">' . 'Last' . '</a></li>';

            echo $trang1;
        }
    }

}
